package com.control.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.entity.Person;
import com.model.imp.*;
import com.model.util.DateUtil;
import com.model.util.PageController;

import com.model.entity.Student;
import com.model.imp.StudentImp;

public class StudentAdminServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		       response.setContentType("text/html; charset=UTF-8");
	    	   this.doPost(request, response);
	}
 public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1 调用数据库，得到列表数据
		String cNumber = request.getParameter("currentPageNo");
		String toNumber = request.getParameter("toPageNo");
		int start = 0;
		if(request.getAttribute("currentPageNo")!=null){
			int pageNumber = Integer.parseInt((String)request.getAttribute("currentPageNo")); 
			if(pageNumber==1){
				start = 1;
			}
		}
		if(cNumber != null && toNumber != null){
			int tn  = Integer.parseInt(toNumber);
			//根据数据库中数据总数，判断所有结果显示的总页数
			//如果总页数太小，设初始值为 1
			if(tn>=0){
				start = tn;
			}
		}
		StudentImp dao = new StudentImp();
		int   total = dao.getTotal();
		PageController pc = new PageController(total, start);
		Collection<Student> list = dao.getStudents(pc.getPageStartRow(), pc.getPageSize());
		//2 把 list 对象，封装到 request 作用域对象中
		request.setAttribute("list", list);
		request.setAttribute("pageController", pc); //控制分页的对象
		//以上是分页部分代码
		String action = request.getParameter("action");
		//添加处理
		if("add".equalsIgnoreCase(action)){
			String stuName=request.getParameter("stuName");
			String stuNumber=request.getParameter("stuNumber");
			String password=request.getParameter("password");
			String stuBirthday=request.getParameter("stuBirthday");
			String stuSex=request.getParameter("stuSex");
			int sex=0;
			if("男".equals(stuSex)){
				 sex=1;
			}else  {sex=2;}
			String stuPolity=request.getParameter("stuPolity");
			int polity=Integer.parseInt(stuPolity);
        	String stuBrief=request.getParameter("stuBrief");
        	Date date=DateUtil.changeStringToDate(stuBirthday);
        	
        	if(date!=null){
        		Student student=new Student(stuName, password, stuNumber,date, sex, polity, stuBrief, 3, 1);
        		student.setBirthday(date);
        		if(student!=null){
        			StudentImp sp=new StudentImp();
        			if(sp.add(student)){
        				request.getSession().setAttribute("msg","添加成功");  
                			request.getRequestDispatcher("studentAdmin.jsp").forward(request, response);
        			}else {request.getSession().setAttribute("msg","添加失败");}
        			
        		}else {request.getSession().setAttribute("msg","添加信息有误");}
        		
        	}else {request.getSession().setAttribute("msg", "日期格式有问题");}

		//查询处理
		}else if("select".equalsIgnoreCase(action)){
			String studentName=request.getParameter("studentName");
			String studentNumber=request.getParameter("studentNumber");
			System.out.println(studentName);
			System.out.println(studentNumber);
			if(studentName!=""){
				    StudentImp sp=new StudentImp();
					Student student=sp.findByName(studentName);
					if(student!=null){
			  
					   request.setAttribute("student", student);
					   request.getRequestDispatcher("studentdetail.jsp").forward(request, response);
					   return ;  
					}else {
						request.getRequestDispatcher("studentAdmin.jsp").forward(request, response);
						}
			}
			if(studentNumber!=""){
				
				  StudentImp sp=new StudentImp();
				 Student student=sp.findByNumber(studentNumber);
				 System.out.println("2");
				 System.out.println(student.getBirthday().toString());
				 if(student!=null){
					   request.setAttribute("student", student);
					   System.out.println("22");
					   request.getRequestDispatcher("studentdetail.jsp").forward(request, response);
					   return ;
					}
			    else{	
				request.getRequestDispatcher("studentAdmin.jsp").forward(request, response);	
		     	}
		  
		   }
			//删除处理  
		}else if("del".equalsIgnoreCase(action)){
			String stuName=request.getParameter("studentName");
			String stuNumber=request.getParameter("studentNumber");
			System.out.println(stuName);
			System.out.println(stuNumber);
			if(stuName!=""&&stuNumber!=""){	
				StudentImp sp=new StudentImp();
				Student student=sp.findByStudent(stuNumber, stuName);
				//System.out.println( student.getState());
				if( student!=null&&sp.delStudent(student)){ 
					  
					request.getSession().setAttribute("msg", "删除成功！");
						request.getRequestDispatcher("studentAdmin.jsp").forward(request, response);
					    
				}else {
						 request.getSession().setAttribute("msg", "删除出错！");
						request.getRequestDispatcher("studentAdmin.jsp").forward(request, response);
						}
			}else{
				 request.getSession().setAttribute("msg", "输入用户有问题！");    
				   request.getRequestDispatcher("studentAdmin.jsp").forward(request, response);		

			      }	
		     
		}else {request.getRequestDispatcher("studentAdmin.jsp").forward(request, response);}
		
		
  }
	
}
